package com.zr.sparesubclass.controller;

import com.zr.sparesubclass.model.*;
import com.zr.sparesubclass.service.SubClassService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 86151 on 2018/12/29.
 */
@RestController
public class SpareSubClassController {
    @Autowired
    private SubClassService subClassService;

    @PostMapping("metadata/getPullDownList")
    @CrossOrigin
    public ResultVO<List<SubClassStatus>> getPullDownList(){
        List<SubClassStatus> subClassStatusList = new ArrayList<>();
        SubClassStatus subClassStatus = new SubClassStatus();
        subClassStatus.setLabel("启用");
        subClassStatus.setValue(true);
        subClassStatusList.add(subClassStatus);
        SubClassStatus subClassStatus1 = new SubClassStatus();
        subClassStatus1.setLabel("禁用");
        subClassStatus1.setValue(false);
        subClassStatusList.add(subClassStatus1);
        return ResultVOBuilder.success(subClassStatusList);
    }

    @PostMapping("subClass/queryPage")
    @CrossOrigin
    public ResultVO<AllRecords> queryPage(@RequestBody SubClass subClass){

        return subClassService.queryPage(subClass);
    }

    @PostMapping("subClass/add")
    @CrossOrigin
    public ResultVO<AddSubClassVo>  add(@RequestBody AddSubClassVo addSubClassVo){
        subClassService.add(addSubClassVo);
        return ResultVOBuilder.success(addSubClassVo);
    }
    @PostMapping("subClass/updateStatus")
    @CrossOrigin
    public ResultVO<UpdateStatusVo>  updateStatus(@RequestBody UpdateStatusVo updateStatusVo){
        subClassService.updateStatus(updateStatusVo);
        return ResultVOBuilder.success(updateStatusVo);
    }
    @PostMapping("subClass/update")
    @CrossOrigin
    public ResultVO<UpdateSubClassVo>  update(@RequestBody @Valid UpdateSubClassVo updateSubClassVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return subClassService.update(updateSubClassVo);
    }
}
